<template>
  <div class="system-btn-container standard-container layout-padding layout-pd">
    <div class="review-list-title">
      <div class="title-actions">
        <el-button size="default" type="primary" class="ml10" @click="refresh">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="primary" class="ml10" @click="handleBatchDeletePer">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          批量删除
        </el-button>
        <el-button size="default" type="primary" class="ml10" @click="onOpenAddPer('add')">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增权限
        </el-button>
      </div>
    </div>
    <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%"
              @selection-change="handleSelectionPer">
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="permissionTypeName" label="权限分类" show-overflow-tooltip></el-table-column>
      <el-table-column prop="permissionName" label="权限名称" show-overflow-tooltip></el-table-column>
      <el-table-column prop="permissionCode" label="权限标识" show-overflow-tooltip></el-table-column>
      <!--      <el-table-column prop="describe" label="权限描述" show-overflow-tooltip></el-table-column>-->
      <el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip></el-table-column>
      <el-table-column prop="updateUsername" label="操作人" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" text type="primary" @click="onOpenEditPer('edit', scope.row)"
          >修改
          </el-button
          >
          <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          :page-sizes="[10, 15, 20, 30, 50, 100]"
          v-model:current-page="state.tableData.paging.currentPage"
          background
          v-model:page-size="state.tableData.paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.paging.totalCount"
      >
      </el-pagination>
    </div>

    <PermissionDialog ref="roleDialogRef" @refresh="refresh()"/>
  </div>
</template>

<script setup lang="ts" name="systemPermission">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import {permissionApi} from '/@/api/sys/permission';
import msg from '/@/utils/messageUtils';
// 引入组件
const PermissionDialog = defineAsyncComponent(() => import('/@/views/system/permission/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive({
  search: '',
  choiceIds: [],
  tableData: {
    data: [],
    total: 0,
    loading: false,
    paging: {
      currentPage: 1,
      pageSize: 15,
      totalCount: 0,
      totalPages: 0,
    },
  },
});
// 初始化表格数据
const getTableData = async () => {
  state.tableData.loading = true;
  try {
    const {data} = await permissionApi().searchPermission({
      permissionName: state.search,
      offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
      limit: state.tableData.paging.pageSize,
    });
    state.tableData.data = data.datas;
    state.tableData.paging.currentPage = data.paging.currentPage;
    state.tableData.paging.totalCount = data.paging.totalCount;
  } catch (error) {
    msg().error('操作失败,请重试');
  }

  state.tableData.loading = false;
};

const handleBatchDeletePer = () => {
  if (state.choiceIds.length == 0) {
    msg().warn('请至少选中一个');
    return;
  }
  ElMessageBox.confirm(`此操作将永久删除选中的权限，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await permissionApi().batchDeletePermission({
          permissionIds: state.choiceIds,
        });
        refresh();
        msg().success('删除成功....');
      })
      .catch(() => {
      });
};

const handleSelectionPer = (choiceIds: any) => {
  state.choiceIds = [];
  choiceIds.forEach((e) => state.choiceIds.push(e.id));
};

const onOpenAddPer = (type: string) => {
  roleDialogRef.value.openDialog(type);
};

const onOpenEditPer = (type: string, row: Object) => {
  roleDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowPermissionType) => {
  ElMessageBox.confirm(`此操作将永久删除权限名称：“${row.permissionName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await permissionApi().deletePermission(row.id);
        ElMessage.success('删除成功');
        getTableData();
      })
      .catch(() => {
      });
};
// 分页改变
const onHandleSizeChange = (pageSize: number) => {
  state.tableData.paging.pageSize = pageSize;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
  state.tableData.paging.currentPage = currentPage;
  getTableData();
};

const refresh = () => {
  state.tableData.paging.currentPage = 1;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-btn-container {
  padding: 15px;

  .el-table {
    flex: 1;
  }
}
</style>
